package com.scs.application.modules.wm.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.modules.sys.model.RestResult;
import com.scs.application.modules.wm.dto.CheckItemDTO;
import com.scs.application.modules.wm.entity.StockCheckItem;
import com.scs.application.modules.wm.mapper.StockCheckItemMapper;
import com.scs.application.modules.wm.service.StockCheckItemService;
import com.scs.application.core.service.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 */
@Service
public class StockCheckItemServiceImpl extends BaseServiceImpl<StockCheckItemMapper, StockCheckItem> implements StockCheckItemService {

    @Override
    public RestResult getStockSumByMatr(String checkId, String matrId, String locationId){
        RestResult result = RestResult.ok();
        Map<String, String> map = new HashMap<>();
        StockCheckItem stockCheckItem = this.list(Wrappers.<StockCheckItem>query().eq("check_id", checkId)
                .eq("location_id", locationId).eq("matr_id", matrId)).get(0);
        //已盘明细
        int checkStatus = 0;
        String  ready = "";
        List<CheckItemDTO> stockReadyCheckItems = baseMapper.getStockSumByMatr(checkId,matrId,locationId,checkStatus);
        if(stockReadyCheckItems != null && stockReadyCheckItems.size() > 0){
            for (CheckItemDTO checkItemDTO : stockReadyCheckItems){
                ready = ready +" " + checkItemDTO.getQty() + checkItemDTO.getPackageUnit();
            }
        } else {
            ready = "无";
        }
        //未盘明细
        checkStatus= -9;
        String  lost = "";
        List<CheckItemDTO> stockLostCheckItems =  baseMapper.getStockSumByMatr(checkId,matrId,locationId,checkStatus);
        if(stockLostCheckItems != null && stockLostCheckItems.size() > 0){
            for (CheckItemDTO checkItemDTO : stockLostCheckItems){
                lost = lost +" " + checkItemDTO.getQty() + checkItemDTO.getPackageUnit();
            }
        } else {
            lost = "无";
        }
        map.put("matrName",stockCheckItem.getMatrName());
        map.put("matrSpec",stockCheckItem.getMatrSpec());
        map.put("locationName",stockCheckItem.getLocationName());
        map.put("ready", ready);
        map.put("lost", lost);
        result.setData(map);
        return result;
    }
}
